﻿using Rhino.Mocks;

namespace Mockery.Core
{
    /// <summary>
    /// Standard set of event handlers for mockery
    /// </summary>
    public class MockeryStateEvents : IMockeryStateEvents
    {
        private readonly MockRepository _mockRepository;
        private readonly IDisposableContextState _disposableContextState;

        /// <summary>
        /// standard ctor
        /// </summary>
        /// <param name="mockRepository">the rhinomocks repository instance</param>
        /// <param name="disposableContextState">container of context that needs to be disposed</param>
        public MockeryStateEvents(MockRepository mockRepository, IDisposableContextState disposableContextState)
        {
            _mockRepository = mockRepository;
            _disposableContextState = disposableContextState;
        }

        #region IMockeryStateEvents Members

        /// <summary>
        /// Arrangement handler that launches the record process
        /// </summary>
        public virtual void OnFirstArrange()
        {
            _mockRepository.Record();
        }

        /// <summary>
        /// Act handler that launches the playback process
        /// </summary>
        public virtual void OnFirstAct()
        {
            _mockRepository.ReplayAll();
        }

        /// <summary>
        /// Assert handler that verifies expectations before performing tests
        /// </summary>
        public virtual void OnFirstAssert()
        {
            _mockRepository.VerifyAll();
            _disposableContextState.Dispose();
        }

        #endregion
    }
}
